const {advertiseModel}=require('../modules/advertise.js');
const { uploadFiles, moveFiles, deleteFiles } = require('../utils/handleFiles');
const path = require('path');

//添加广告位租赁合同
async function addAdvertise(req, res, next){
    const data=req.body;
    if (data.accessory) {
        // 获取路径中的图片名称
        const filename = path.basename(data.accessory);
        // 移动图片
        moveFiles({
            fromPath: './public/temp',
            toPath: './public/images',
            filename
        })
           // 删除临时文件夹
        deleteFiles('./public/temp');
        // 更改图片路径的数据
        data.accessory= 'http://localhost:3100/./images/' + filename;
    }

    const result=await advertiseModel.create(data);
    const result2=await advertiseModel.find({_id:result._id}).populate('advertiseportId');
    if(result){
        res.send({
            message:"添加广告位租赁合同成功",
            code:200,
            data:result2[0]
        });
    }else{
        res.send({
            message:"添加广告位租赁合同失败",
            code:500
        });
    }
}



//修改广告位租赁合同
async function updateAdvertiseRent(req, res, next){
    const data=req.body;
    if (data.accessory) {
        // 获取路径中的图片名称
        const filename = path.basename(data.accessory);
        // 移动图片
        moveFiles({
            fromPath: './public/temp',
            toPath: './public/images',
            filename
        })
           // 删除临时文件夹
        deleteFiles('./public/temp');
        // 更改图片路径的数据
        data.accessory= 'http://localhost:3100/./images/' + filename;
    }
    const _id=data._id;
    const result=await advertiseModel.updateOne({_id},data);
    if(result.ok>0){
        res.send({
            message:"更新车位租赁合同成功",
            code:200
        });
    }else{
        res.send({
            message:"更新车位租赁合同失败",
            code:500
        });
    }
}




//模糊查询广告位合同
async function  getAdvertiseRent(req, res, next){
    const data=req.query;
    const value=data.searchObj.value;
    const currentPage=data.currentPage;
    const pageSize=data.pageSize;
    const result=await advertiseModel.find({$or:[{leaseeTel:{$regex:value,$options:'$i'}},{leaseeContacts:{$regex:value,$options:'$i'}},{locations:{$regex:value,$options:'$i'}},{leasee:{$regex:value,$options:'$i'}}]}).populate('communityId').populate('advertiseportId').limit(Number(pageSize)).skip((Number(currentPage)-1)*Number(pageSize));
    const total=await advertiseModel.find({$or:[{leaseeTel:{$regex:value,$options:'$i'}},{leaseeContacts:{$regex:value,$options:'$i'}},{locations:{$regex:value,$options:'$i'}},{leasee:{$regex:value,$options:'$i'}}]}).countDocuments();
    const pages=Math.ceil(total/pageSize);

    if(result.length>0){
        res.send({
            message:"获取客户数据成功",
            code:200,
            result,
            total,
            pages
        });
    }else{
        res.send({
            message:"获取客户数据失败",
            code:500
        });  
    }
}


//删除租赁广告位合同数据
async function delAdvertiseData (req, res, next) {
    const data=req.body;
    const result=await advertiseModel.deleteOne(data);
    if(result.deletedCount>0){
        res.send({
            message:"删除赁车位合同数据成功",
            code:200,
        });
    }else{
        res.send({
            message:"删除赁车位合同数据失败",
            code:500
        });  
    }

}




//通过ID获取广告位租赁合同数据
async function getAdvertiseById (req, res, next) {
    const _id=req.params;
    const result=await advertiseModel.find({_id}).populate('communityId').populate('advertiseportId');
    if(result.length>0){
        res.send({
            message:"获取车位租赁合同数据成功",
            code:200,
            result:result[0]
        });
    }else{
        res.send({
            message:"获取车位租赁合同数据失败",
            code:500
        });  
    }
}


function loadImg(req, res, next) {
        // 图片上传
        const uploadImages = uploadFiles({
            // 图片上传成功后的存储路径
            path: './public/temp',
            // key: 'file',
            size: 1000000
        });
        uploadImages(req, res, (error) => {
            if(error) {
                console.log('图片上传失败', error);
            } else {
                console.log('图片上传成功');
                // 输出上传成功后的图片信息
                res.send({
                    message: '图片上传成功',
                    code: 200,
                    data: './temp/' + req.files[0].filename
                })
            }
        });
}


module.exports={addAdvertise,updateAdvertiseRent,getAdvertiseRent,delAdvertiseData,getAdvertiseById,loadImg}